The present invention relates generally to the field of computer software and software portability. In particular, it relates to the construction dispatch tables.
To facilitate the following discussion, a brief overview of a conventional object oriented computing environment will be given. In an object oriented computing environment, an object refers to a software bundle of variables and related methods. An object maintains its state, i.e., what the object knows, in variables, and implements its behavior, i.e. what the object can do, in methods. Messages are the means by which software objects interact and communicate with each other. Collectively, variables and methods may be referred to as members. The variables and methods for an object are also referred to as instance variables and instance methods.
Typically, class variables and class methods are those variables and methods defined as belonging to a class. A class may be described as a reusable blueprint that defines the variables and the methods common to all objects of a certain kind. An instance is an object belonging to a class where memory is allocated for the instance variables in the class.
Classes may be arranged in a class hierarchy, or inheritance tree, in which the further down the hierarchy a class appears, the more specialized it is. FIG. 1 is a block diagram representing a particular class hierarchy 100. The class hierarchy 100 includes a Class A 102 including variables 104 and methods 106. In the illustrated embodiment, Class A has a plurality of associated methods, including method M. The class hierarchy further includes Class B 110 which inherits from Class A, and Classes C, D and E (112,114 and 116 respectively), which all inherit from Class B 110. Each of the subclasses also include variables 104 and methods 106.
A superclass refers to the class"" direct ancestor as well as to all of its ascendant classes. A xe2x80x98directxe2x80x99 superclass distinguishes the immediate parent superclass from the alternate ascendant superclasses in a class hierarchy. A subclass provides specialized behavior apart from the basis of common elements provided by the superclass. To do this, each subclass inherits variables and methods from the superclass. In addition, subclasses can add variables and methods to the ones they inherit from the superclass. For example, the class B 110 inherits accessible methods defined in its superclass (class A 102), including the method M 108. Class B may also define its own new methods, including method N, 109 in the embodiment shown. It is important to note that subclasses can also override one or more of its inherited methods and provide specialized implementations for those methods.
When a language allows classes to inherit from a single superclass, it is said to have a single inheritance. In contrast, when a language allows classes to inherit from more than one superclass, it is said to have multiple inheritance. In the class hierarchy illustrated in FIG. 1 there is single inheritance since each subclass inherits from just one direct superclass.
In some languages, a superclass may be repetitively used through the practice of inheritance. Generally, a subclass inherits all of the methods from its superclass that are accessible to that subclass as they are unless the subclass explicitly overrides a method. Accessibility is determined by a combination of an accessibility declaration of the superclass member and an accessibility, declaration of the subclass member.
Accessibility is differentiated to facilitate a spectrum from unrestricted sharing of source code to specific forms of privacy. The two conventional types of accessibility levels are public and private. A public member can be seen or accessed by any other class. A private member can only be accessed by its own class. Particular languages may use additional accessibility types. For example, C++ uses a protected accessibility in which a protected member can generally only be accessed by its subclasses. Additionally, Java uses a package private accessibility in which a package private member can only be accessed by classes in a particular package. A package is a collection of related classes and interfaces that provide access protection and namespace management for all the elements of the package. Classes may be grouped into packages to make classes easier to find and use, to avoid naming conflicts, and to control access.
Generally, the accessibility of a message is specified upon declaration of the member. If no accessibility is initially provided, a default accessibility may be used. For example, within a Java package, the default accessibility for a member whose accessibility is not initially specified becomes package private.
FIG. 2 is a block diagram describing a class hierarchy, wherein each class in the hierarchy locally defines a method xe2x80x9cfooxe2x80x9d. Specifically, class A 202 belongs to a Package P1 and contains a method xe2x80x98A.fooxe2x80x99 204 having an unspecified accessibility. In this case, the method A.foo 204 is defaulted to a package private accessibility with respect to Package P1. Similarly, class B 206 belongs to a Package P2 and contains a method xe2x80x98B.fooxe2x80x99 208 having a public accessibility. Class C 210 belongs to a Package P1 and contains a method xe2x80x98C.fooxe2x80x99 212 having a public accessibility. The methods 204, 208 and 212 all implement the method xe2x80x9cfooxe2x80x9d, however, what differs between the three is their respective class, package, source code and accessibility. This difference may affect the behavior of the method, e.g. if the method calls source code for a print statement to announce the method""s class, the three methods will have different outputs as illustrated.
Conventionally, every class and interface has a method table containing all the methods it locally defines. In addition, every class has a dispatch table, or Vtable, that has entries for all externally accessible methods that can be invoked by the class, including inherited methods. Typically private methods are not included in the Vtable. In conventional Vtable construction, every externally accessible method is associated with a single Vtable entry, which points to a section of code corresponding to a method. Thus, every Vtable entry points back at a method it corresponds to, whether local or inherited.
FIG. 3 is a block diagram illustrating a conventional format of a Vtable 300 corresponding to class B 206 of FIG. 2. The Vtable 300 includes a superclass portion 302 which contains all the entries corresponding to methods inherited from the direct superclass (class A 202) and a class portion 304 which contains the entries corresponding to the methods locally defined in class B 206. The locally defined methods found in the class portion 304 are those which are new to class B 206. For example, a new entry 306 which did not over-write an entry corresponding to a method from class A 202 is added in the class portion 304 and thus increases the size of the Vtable 300. Alternately, locally defined methods that override a corresponding superclass method overwrite the existing Vtable entry in the superclass portion 302. For example, an entry 308 of Vtable 300 which points to code for B.foo overwrites the corresponding entry in the superclass portion of Vtable 300 to point to the code for the locally defined method C.foo.
The Java programming language contemplates the use of single inheritance. In Java, a subclass is defined to inherit all of the accessible members of its superclass and ancestors and can use these members as is, hide them or override them. Subclasses inherit those superclass members declared as public or protected. In addition, subclasses inherit a superclasses"" members declared with no access designation as long as the subclass is in the same package as the superclass (i.e. they both default to package private). Thus, Java flexibly provides an accessibility spectrum from unrestricted sharing of source code to controllable levels of privacy. Conventional Vtable construction based on a diametric scheme for inheritance does not permit this accessibility spectrum.
In view of the foregoing, it should be apparent that improved dispatch table construction techniques would be desirable.
According to the present invention, methods, apparatus, and computer program products are disclosed for constructing dispatch tables. In one embodiment of the present invention, the decision to allocate a new dispatch tables entry is sensitive to accessibility. In another embodiment, a Vtable is provided wherein a method in the Vtable has more than one entry.
The invention relates in accordance with one embodiment to a process of building a dispatch table for methods in a class that inherits from a single superclass. The process includes copying a dispatch table for a superclass. The process also includes determining whether a selected method in the class also exists in an ancestor superclass of the class. The process further includes determining whether a selected superclass version of the selected method is accessible when it is determined that the selected method also exists in an ancestor superclass that the first class inherits from. The process additionally includes creating a new entry in the dispatch table for the selected method in the first class when it is determined that the selected superclass version of the selected method is not accessible. The process also includes overwriting an entry when it is determined that the selected method in the first class is accessible. When no superclass version of the method exists, a new entry for the method is created.
In another embodiment, the invention relates to a process of building a dispatch table for methods in a class that inherits from a direct superclass in an ascendant hierarchy. The process includes copying a dispatch table from a superclass. The process also includes determining whether a selected method and accessibility in the class also exist in a superclass of the ascendant hierarchy. The process further includes assigning an index to an entry of the dispatch table.
In yet another embodiment, the invention relates to a dispatch table for a class that inherits from at least one superclass, the dispatch table including a plurality of entries for a particular method.